package stopthread;

import org.junit.jupiter.api.Test;

/**
 * 能停止的线程-异常法
 */
public class Demo_03 {
    /**
     * 只要for循环没结束，就会继续执行
     */
    @Test
    public void stopThread_01() {
        try {
            Stop1 thread = new Stop1();
            thread.start();
            Thread.sleep(10);
            thread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("end!");

    }

    /**
     * 可以停止
     */
    @Test
    public void stopThread_02() {
        try {
            Stop2 thread = new Stop2();
            thread.start();
            Thread.sleep(10);
            thread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("end!");

    }

    @Test
    public void stopSleepThread_01() {
        Stop3 thread = new Stop3();
        thread.start();
        thread.interrupt();
        System.out.println("end!");
    }

    /**
     * 在沉睡中停止线程
     */
    @Test
    public void stopSleepThread_02() {
        try {
            Stop4 thread = new Stop4();
            thread.start();
            Thread.sleep(200);
            thread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Stop1 extends Thread {
    @Override
    public void run() {
        for(int i = 0; i < 50000; i++) {
            if(this.isInterrupted()) {
                System.out.println("已经是停止状态了，我要退出了");
                break;
            }
            System.out.println("i=" + (i + 1));
        }

        System.out.println("for循环后继续运行");

    }
}

class Stop2 extends Thread {
    @Override
    public void run() {
        try {
            for (int i = 0; i < 50000; i++) {
                if (this.isInterrupted()) {
                    System.out.println("已经是停止状态了，我要退出了");
                    throw new InterruptedException();
                }
                System.out.println("i=" + (i + 1));
            }
        } catch (InterruptedException e) {
            System.out.println("跳出for循环了");
            e.printStackTrace();
        }

        System.out.println("for循环没有停止");
    }
}

class Stop3 extends Thread {
    @Override
    public void run() {
        try {
            for(int i = 0; i < 1000000; i++) {
                System.out.println("i=" + (i + 1));
            }

            System.out.println("run begin");
            Thread.sleep(20000);
            System.out.println("run end");
        } catch (InterruptedException e) {
            System.out.println("先停止，再遇到sleep，进入catch");
            e.printStackTrace();
        }
    }
}

class Stop4 extends Thread{
    @Override
    public void run() {
        try {
            System.out.println("run begin");
            Thread.sleep(200000);
            System.out.println("run end");
        } catch (InterruptedException e) {
            System.out.println("在沉睡中进入Catch");
            e.printStackTrace();
        }
    }
}

